/*******************************************************************************
																				
	DESCRIPTION:  	This file processes the results fro the duration dependence
					regressions and saves them in Stata format.

*******************************************************************************/

clear all
global id_code 119_3
pause on
set seed 2110


* Set globals:
global model Full	
global y0 2006
global y1 2006

if $y0 != $y1 {
	global y "Pooled_${y0}_${y1}"
}
else if $y0 == $y1 {
	global y $y0
}
		

*******************************************************************************
* Clean results from R
*******************************************************************************
local pred
	
* Import the coefficients and merge with the shrunken predictions:	
frame create betas`pred'
frame betas`pred': import delimited "${data}/119_2_DurationDependenceBetas_Full_${y}`pred'.csv", clear	

frame change default
use "${data}/119_1_DataForR_DurationDependenceBetas_Full_${y}_MergeOn.dta", clear
frlink 1:1 key, frame(betas`pred')
frget beta_0 beta_0_se beta_1 beta_1_se, from(betas`pred')

* Calculate statistics
foreach x in beta_0 beta_1 {
	
	* Mean
	egen `x'_mean = mean(`x')
	
	* Variance
	egen `x'_sd = sd(`x')
	gen `x'_var = (`x'_sd)^2
	
	* Shrunken variance (ind. level)
	gen `x'_svar_ind = max(`x'_var - (`x'_se)^2, 0)	
	
	* Mean standard error
	egen `x'_se_mean = mean(`x'_se)			
	
	* Shrunken variance (pop. level)
	gen `x'_svar_pop = `x'_var - (`x'_se_mean)^2
	
	* Shrunken variable (ind. level)
	gen `x'_shrunk_ind = `x'_mean + ((`x'_svar_ind/`x'_var)^0.5)*(`x' - `x'_mean)
	
	* Shrunken variable (pop. level)
	gen `x'_shrunk_pop = `x'_mean + ((`x'_svar_pop/`x'_var)^0.5)*(`x' - `x'_mean)
		
}
	
* Save:
save "${data}/${id_code}_DurationDependenceBetas_Full_${y}`pred'.dta", replace

drop beta*
